from ebl_pattern_writer import point, shape, field

class waveguidefield(field):
    def __init__(self,fieldsize,nx,ny,writemarkers=True):
        field.__init__(self,fieldsize)
        fieldsize=1000*fieldsize # since field.__init__ does this to self.fieldsize, I want them to agree to avoid confusion
        
        marker_s=500
        width0=2000
        widthstep=2000
        height0=4000
        heightstep=4000
        widths=range(width0,width0+(nx-1)*widthstep+1,widthstep)
        heights=range(height0,height0+(ny-1)*heightstep+1,heightstep)
        
        
        # gap=8000
        # spacing=point(gap,gap)
        # w0=2000
        # wstep=2000
        # h0=4000
        # hstep=4000
        
        if writemarkers:
            spacing=point((self.fieldsize-sum(widths))/nx,(self.fieldsize-sum(heights))/ny)
        else:
            spacing=point((self.fieldsize-sum(widths))/(nx-1),(self.fieldsize-sum(heights))/(ny-1))
            
        if writemarkers:
            for ii in range(nx):
                for jj in range(ii+1):
                    nextshape=bar(marker_s,2*marker_s)
                    nextshape.translate(spacing.x*(ii+1)+sum(widths[0:ii])+marker_s*(2*jj+0.5),-marker_s)
                    self.shapes.append(nextshape)
            for ii in range(ny):
                for jj in range(ii+1):
                    nextshape=bar(marker_s,marker_s)
                    nextshape.translate(marker_s/2.0,-(spacing.y*(ii+1)+sum(heights[0:ii])+marker_s*(2*jj+0.5)))
                    self.shapes.append(nextshape)
        
                
        # write guides
        for ii in range(nx):
           for jj in range(ny):
               nextshape=bar(widths[ii],heights[jj])
               nextshape.translate(sum(widths[0:ii])+widths[ii]*0.5+spacing.x*(ii+writemarkers),\
                                 -(sum(heights[0:jj])+heights[jj]*0.5+spacing.y*(jj+writemarkers)))
               self.shapes.append(nextshape)

        # x=0
        # w=w0
        # jj=1
        # while (x+spacing.x+w) < self.fieldsize:
            # y = 0
            # h=h0
            # ii=1
            # for step in range(jj):
                # nextshape=bar(marker_s,2*marker_s).translate(x+spacing.x+marker_s*(2*step+0.5),-marker_s)
                # self.shapes.append(nextshape)
            # ii=ii+1

            # while (y+spacing.y+h) < self.fieldsize:
                # if x==0 :
                    # for step in range(ii):
                        # nextshape=square(marker_s).translate(marker_s*0.5,-(y+spacing.y+marker_s*(2*step+0.5)))
                        # self.shapes.append(nextshape)
                    # ii=ii+1
                # nextshape=bar(w,h)
                # nextshape.translate(x+spacing.x+w*0.5,-(y+spacing.y+h*0.5))
                # self.shapes.append(nextshape)
                # h = h + hstep
                # y=y+spacing.y+h
            # w = w + wstep
            # x=x+spacing.x+w
            # jj=jj+1
                
print('done')                
                
myfield=waveguidefield(120,5,5)
#myfield.writematlab()
#myfield.writexml()
#myfield.writeccc()
myfield.writexmldom()
